import json
from src.storage.file_manager import save_to_file,load_from_file
from .id_generator import *

def delete_item(items, item_id):
    """Delete item by ID"""
    if item_id in items:
        del items[item_id]
        save_to_file(items)
        print(f"Item with ID {item_id} deleted successfully!")
    else:
        print(f"Item with ID {item_id} not found!")
    return items

def display_items(items):
    """Display all items"""
    if not items:
        print("No items in collection.")
        return
    
    print(f"\n{'='*30}")
    print(f"Items: {len(items)}/4")
    print('='*30)
    

    
    for i, item in enumerate(items.values(), start=1):
        print(f"\n{i}. ID: {item['ID']}")
        print(f"   Type: {item['type']}")
        print(f"   Name: {item['name']}")
        print(f"   Character: {item['character']}")
        print(f"   Property: {item['property']}")
        print(f"   Ingredient: {item['ingredient']}")

def create_item(items, bread_type=None, name=None, character=None, property=None, ingredient=None):
    """Add new item to the list"""
    if len(items) >= 4:
        print ("Maximum 4 items allowed--no more can be added")
        return items
    
    if all([bread_type, name, character, property, ingredient]):
        new_item = {
            "ID": generate_id(),
            "type": bread_type,
            "name": name,
            "character": character,
            "property": property,
            "ingredient": ingredient
        }
        items[new_item["ID"]] = new_item
        save_to_file(items)
        print(f"Created successfully!Now you have {len(items)}/4 items")
        if len(items) >= 4:
            print("The warehouse is full")
        return items

    while len(items) < 4:
        print ("\n===PLEASE INPUT YOUR NEW ITEM'S INFORMATION===")
        while True:
            type = input("What is your bread's type(example:Chocolate Lava Croissant)?")
            if type:
                break
            print ("Your type can not be empty")
        while True:
            name = input("What is your bread's name(example:Crispy)?")
            if name:
                break
            print ("Your name can not be empty")
        while True:
            character = input("What is your bread's character(example:open)?")
            if character:
                break
            print ("The character can not be empty")
        while True:
            property = input("What is your bread's property(example:fire)?")
            if property:
                break
            print ("The property can not be empty")
        while True:
            ingredient = input("What is your bread's ingredient(example:milk.etc)?")
            if ingredient:
                break
            print ("The ingredient can not be empty")

        new_item = {
            "ID": generate_id(),
            "type": type,
            "name": name,
            "character": character,
            "property": property,
            "ingredient": ingredient
        }
        items[new_item["ID"]] = new_item
        save_to_file(items)
        print (f"Created successfully!Now you have {len(items)}/4 items")

        if len(items) < 4:
            choice = input ("\nDo you want create another one?(yes/no)").strip().lower()
            if choice != "yes":
                break
    if len(items) >= 4:
        print ("The warehouse is full")
    return items

if __name__ == "__main__":
    print("Testing delete_item...")
    test_items = [
        {"ID": "abc123", "type": "chocolate", "name": "Crispy", "character": "open and outgoing", "property": "fire magic", "ingredient": "butter from grassland pastures, flour of cloud-kissed wheat, and the magical milk—their blend weaves a delicious spell."},       
        {"ID": "def456", "type": "cheese", "name": "Creamout", "character": "aloof yet tender", "property": "ice magic", "ingredient": "iceberg-harvested cream, cloud village's day-and-night fermented blend, high-end wheat flour."}
    ]
    display_items(test_items)
    test_items = delete_item(test_items, "abc123")
    display_items(test_items)

    test_items = {}
    test_items = create_item(test_items)

